﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using VocabManager.View;
using VocabManager.ViewModel;

namespace VocabManager
{
    public partial class MainPage : PhoneApplicationPage
    {

       
        internal ApplicationBarIconButton _addNewButton;
        internal ApplicationBarIconButton _deleteCurrentButton;
        internal ApplicationBarIconButton _editButton;
        internal ApplicationBarIconButton _selectButton;

        //one time views which are created just one time, stored and used  
        AppListView _deckListView;
        AppListViewModel _viewModel;
        WelcomeView _welcomeView;
        HelpView _helpView;
        AboutView _aboutView;
     
        public MainPage()
        {
            InitializeComponent();
            InitApplicationButtons();
            LoadWelcomeView();
        }


        //// initialize the application buttons (ones are shared)
        private void InitApplicationButtons()
        {
            _addNewButton = new ApplicationBarIconButton(new Uri(IconUrls.Add, UriKind.Relative));
            _addNewButton.Text = "add a new deck";
            _addNewButton.Click += new EventHandler(_addNewButton_Click);

            _deleteCurrentButton = new ApplicationBarIconButton(new Uri(IconUrls.Delete, UriKind.Relative));
            _deleteCurrentButton.Click += new EventHandler(_deleteCurrent_Click);
            _deleteCurrentButton.Text = "delect selcted deck";
            
            _editButton = new ApplicationBarIconButton(new Uri(IconUrls.Edit, UriKind.Relative));
            _editButton.Click += new EventHandler(_editButton_Click);
            _editButton.Text = "edit deck";
            
            _selectButton = new ApplicationBarIconButton(new Uri(IconUrls.Select, UriKind.Relative));
            _selectButton.Text = "review deck";
            _selectButton.Click += new EventHandler(_selectButton_Click);
            
            this.ApplicationBar.Buttons.Add(_addNewButton);
            this.ApplicationBar.Buttons.Add(_selectButton);
            this.ApplicationBar.Buttons.Add(_editButton);
            this.ApplicationBar.Buttons.Add(_deleteCurrentButton);
        }

        void mnu_Click(object sender, EventArgs e)
        {
            LoadWelcomeView();
        }

        void _selectButton_Click(object sender, EventArgs e)
        {
            _viewModel.ViewSelectedDeckCommand.Execute(null);
        }

        void _editButton_Click(object sender, EventArgs e)
        {
            if (_viewModel.CurrentDeck != null) 
            _viewModel.CurrentDeck.EditCommand.Execute(null);
        }

        void _deleteCurrent_Click(object sender, EventArgs e)
        {
            if (_viewModel.CurrentDeck != null) 
                _viewModel.RemoveCurrentDeckCommand.Execute(null);
        }

        void _addNewButton_Click(object sender, EventArgs e)
        {
            _viewModel.AddNewCommand.Execute(null);
        }

    //The whole application is just one page and each different view is a usercontrol which is hosted in this page
    // based on the need
        public void LoadView( UserControlBase newChild)
        {
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.Children.Add(newChild);
        }
        public void LoadDeckListView()
        {
            if (_deckListView == null)
            {
                // initialize  database and also create AppListViewModel
                this.DataContext = _viewModel = new AppListViewModel(
                new DataLayer.DeckRepository(VocabManager.DataLayer.DatabaseManager.Insatance.DataContext),
                new DataLayer.ItemRepository(VocabManager.DataLayer.DatabaseManager.Insatance.DataContext));
                _deckListView = new AppListView();
                _deckListView.DataContext = _viewModel;
            }
            LoadView(_deckListView);
        }


        public void LoadHelpView()
        {
            if (_helpView == null)
            {
                _helpView = new HelpView();

            }
            LoadView(_helpView);
        }

        public void LoadWelcomeView()
        {
            if (_welcomeView == null)
            {
                _welcomeView = new WelcomeView();

            }
            LoadView(_welcomeView);
        }

        internal void LoadAboutView()
        {
            if (_aboutView == null)
            {
                _aboutView = new AboutView();

            }
            LoadView(_aboutView);
        }

      
       
    }
}